package com.web.cms.pan.util;

import com.blue.core.util.NumberUtil;
import com.blue.integration.serve.Bucket;
import com.blue.integration.serve.StorageClient;
import com.blue.jdbc.Page;
import com.web.cms.pan.message.FolderFileRequest;
import com.web.cms.pan.message.FolderFileResponse;
import com.web.cms.pan.model.FolderFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 目录文件转换器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年04月09日
 */
public class FolderFileConvertor
{
	private FolderFileConvertor()
	{
	}
	
	public static FolderFile toFolderFile(FolderFileRequest request)
	{
		if (request == null)
			return null;
		
		FolderFile folderFile = new FolderFile();
		folderFile.setId(request.getId());
		folderFile.setFolderId(request.getFolderId());
		folderFile.setFileDataId(request.getFileDataId());
		folderFile.setUserId(request.getUserId());
		folderFile.setName(request.getName());
		folderFile.setKey(request.getKey());
		folderFile.setCreateTime(request.getCreateTime());
		folderFile.setDeleteTime(request.getDeleteTime());
		folderFile.setFileSize(request.getFileSize());
		folderFile.setType(request.getType());
		folderFile.setState(request.getState());
		return folderFile;
	}
	
	public static List<FolderFile> toFolderFile(List<FolderFileRequest> requestList)
	{
		List<FolderFile> list = new ArrayList<>();
		if (requestList == null || requestList.isEmpty())
			return list;
		
		for (FolderFileRequest request : requestList)
		{
			list.add(toFolderFile(request));
		}
		return list;
	}
	
	public static FolderFileResponse toResponse(FolderFile folderFile, StorageClient storageClient)
	{
		if (folderFile == null)
			return null;
		
		FolderFileResponse response = new FolderFileResponse();
		response.setId(folderFile.getId());
		response.setFolderId(folderFile.getFolderId());
		response.setFileDataId(folderFile.getFileDataId());
		response.setUserId(folderFile.getUserId());
		response.setName(folderFile.getName());
		response.setCreateTime(folderFile.getCreateTime());
		response.setDeleteTime(folderFile.getDeleteTime());
		response.setType(folderFile.getType());
		response.setFileSize(folderFile.getFileSize());
		response.setState(folderFile.getState());

		response.setFileSizeName(NumberUtil.byteFormat(folderFile.getFileSize()));
		response.setUrl(storageClient.getUrl(Bucket.PAN, folderFile.getKey()));
		return response;
	}
	
	public static List<FolderFileResponse> toResponse(List<FolderFile> list, StorageClient storageClient)
	{
		List<FolderFileResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;
		
		for (FolderFile folderFile : list)
		{
			responseList.add(toResponse(folderFile, storageClient));
		}
		return responseList;
	}
	
	public static Map<Integer, FolderFileResponse> toResponse(Map<Integer, FolderFile> map, StorageClient storageClient)
	{
		Map<Integer, FolderFileResponse> responseMap = new HashMap<>();
		if (map == null || map.isEmpty())
			return responseMap;
		
		for (Map.Entry<Integer, FolderFile> entry : map.entrySet())
		{
			responseMap.put(entry.getKey(), toResponse(entry.getValue(), storageClient));
		}
		return responseMap;
	}
	
	public static Page<FolderFileResponse> toResponse(Page<FolderFile> page, StorageClient storageClient)
	{
		if (page == null)
			return null;
		
		Page<FolderFileResponse> responsePage = new Page<>();
		responsePage.setCurPage(page.getCurPage());
		responsePage.setRows(page.getRows());
		responsePage.setSize(page.getSize());
		responsePage.setStartRowNo(page.getStartRowNo());
		responsePage.setTotalPage(page.getTotalPage());
		responsePage.setObjectList(toResponse(page.getObjectList(), storageClient));
		return responsePage;
	}
	
}
